<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>飞机方向</title>
    <style>
        body{
            margin: 0;
            padding: 0;
            width: 100%;
            background: url("sky.jpg");
        }
        #fly{
            background: url("mes.png") no-repeat center;
            position: absolute;
            top: 50%;
            left: 50%;
        }
    </style>
</head>
<body>
</body>
</html>
<script>
    var fly = document.createElement('div')
    fly.id = 'fly'
    fly.style.width = '60px'
    fly.style.height = '60px'
    document.body.appendChild(fly)

    var skyx = skyt = 0
    var T;

    document.onmousemove = function (e)
    {
        var fly = document.getElementById('fly')
        var x = e.clientX - fly.offsetLeft - (fly.offsetWidth / 2)
        var y = e.clientY - fly.offsetTop - (fly.offsetHeight / 2)

        c = Math.atan2(x,y);//取值
        c = 180 * c / Math.PI; //转换角度
        c = c*-1; //转换方向

        function play()
        {
            clearTimeout(T)
            
            console.log(c)
            fly.style.transform = `rotate(${c}deg)`

            switch(true)
            {
                case c<15&&c>-15://向下飞
                    skyt-=5;
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
                case c>15&&c<75://向左下角飞
                    skyx+=5;
                    skyt-=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
                case c>75&&c<105://向左飞
                    skyx+=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    break;
                case c>105&&c<165://向左上角飞
                    skyx+=5;
                    skyt+=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
                case c>165||c<-165://向上飞
                    skyt+=5;
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
                case c<-15&&c>-75://向右下角飞
                    skyx-=5;
                    skyt-=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
                case c<-75&&c>-105://向右飞
                    skyx-=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    break;
                case c<-105&&c>-165://向右上角飞
                    skyx-=5;
                    skyt+=5;
                    document.body.style.backgroundPositionX=skyx+'px';
                    document.body.style.backgroundPositionY=skyt+'px';
                    break;
            }

            T = setTimeout(play, 10)
        }

        play()
    }
</script>